<template>
  <div class="app-container">
    <h3 style="font-weight: bold;">查询条件</h3>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col span="20" offset="1">
          <el-form-item label="文件编号" prop="F0901">
            <el-input
              v-model="queryParams.F0901"
              placeholder="请输入文件编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="上传人" prop="F0601">
            <el-input
              v-model="queryParams.F0601"
              placeholder="请输入上传人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="文件名称" prop="F0902">
            <el-input
              v-model="queryParams.F0902"
              placeholder="请输入文件名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        
        <el-col span="3" >
          <el-form-item id="query-button">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col span="20" offset="1">
          <el-form-item label="文件格式" prop="F0904">
            <el-select v-model="queryParams.F0904" placeholder="请选择文件格式" clearable>
              <el-option
                v-for="dict in dict.type.client_document_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="上传时间">
            <el-date-picker
              v-model="daterangeF0905"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['knowledge_manage:T09:add']"
        >上传文件</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['knowledge_manage:T09:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['knowledge_manage:T09:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="T09List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件编号" align="center" prop="f0901" />
      <el-table-column label="上传人" align="center" prop="f1801" />
      <el-table-column label="文件名称" align="center" prop="f0902" />
      <el-table-column label="文件格式" align="center" prop="f0904" />
      <el-table-column label="上传时间" align="center" prop="f0905" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.F0905, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['knowledge_manage:T09:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['knowledge_manage:T09:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :before-upload="beforeUpload"
          multiple
          :limit="10"
          :on-exceed="handleExceed"
          :accept="fileType"
          >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传doc/txt文件，且不超过1万字</div>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  #query-button{
    min-width: 160px;
  }
</style>

<script>
import { listT09, getT09, delT09, addT09, updateT09 } from "@/api/knowledge_manage/T09";

export default {
  name: "Knowledge",
  dicts: ['client_document_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识库管理表格数据
      T09List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 文件字数时间范围
      daterangeF0905: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        F0901: null,
        F0601: null,
        F0902: null,
        F0904: null,
        F0905: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 上传文件类型
      fileType:".doc,.docx,.txt"
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询知识库管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeF0905 && '' != this.daterangeF0905) {
        this.queryParams.params["beginF0905"] = this.daterangeF0905[0];
        this.queryParams.params["endF0905"] = this.daterangeF0905[1];
      }
      listT09(this.queryParams).then(response => {
        this.T09List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        F0901: null,
        F0601: null,
        F0301: null,
        F0902: null,
        F0903: null,
        F0904: null,
        F0905: null,
        F0906: null,
        F0907: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeF0905 = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.F0901)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识库管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const F0901 = row.F0901 || this.ids
      getT09(F0901).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改知识库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const F0901s = row.F0901 || this.ids;
      this.$modal.confirm('是否确认删除知识库管理编号为"' + F0901s + '"的数据项？').then(function() {
        return delT09(F0901s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('knowledge_manage/T09/export', {
        ...this.queryParams
      }, `T09_${new Date().getTime()}.xlsx`)
    },
    // 文件上传函数

      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 10 个文件，本次选择了 ${files.length} 个文件`);
      },
      beforeUpload(file){
        const fileExtension = file.name.split('.').pop().toLowerCase();
        console.log("文件扩展名",fileExtension)
        if(!this.fileType.includes(`.${fileExtension}`)){
          this.$message.error('仅允许上传 .doc, .docx 和 .txt 文件！');
          return Promise.reject(flase);
        }
        this.$modal.msgSuccess("上传成功");
        return true;
      }
  }
};
</script>
